/**
 * Project: push-center-biz-core
 * 
 * File Created at 2014年10月9日
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package com.greenline.push.core.pushcenter.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.greenline.push.core.pushcenter.CategoryManager;
import com.greenline.push.core.pushcenter.TemplateManager;
import com.greenline.push.dal.pushcenter.TemplateDao;
import com.greenline.push.dal.pushcenter.dataobject.CategoryDO;
import com.greenline.push.dal.pushcenter.dataobject.TemplateDO;
import com.greenline.push.dal.query.TemplateQuery;

/**
 * @Type TemplateManagerImpl
 * @Desc 
 * @author zhaoli
 * @date 2014年10月9日
 * @Version V1.0
 */
public class TemplateManagerImpl implements TemplateManager {
	@Resource
	private TemplateDao templateDao;
	@Resource
	private CategoryManager categoryManager;

	/* (non-Javadoc)
	 * @see com.greenline.push.core.pushcenter.TemplateManagerImpl#insert(com.greenline.push.dal.pushcenter.dataobject.TemplateDO)
	 */
	@Override
	public Long insert(TemplateDO template) {
		return templateDao.insert(template);
	}

	/* (non-Javadoc)
	 * @see com.greenline.push.core.pushcenter.TemplateManagerImpl#getTemplateById(java.lang.Long)
	 */
	@Override
	public TemplateDO getTemplateById(Long id) {
		return templateDao.getById(id);
	}

	/* (non-Javadoc)
	 * @see com.greenline.push.core.pushcenter.TemplateManagerImpl#update(com.greenline.push.dal.pushcenter.dataobject.TemplateDO)
	 */
	@Override
	public boolean update(TemplateDO template) {
		return templateDao.update(template)  > 0;
	}

	/* (non-Javadoc)
	 * @see com.greenline.push.core.pushcenter.TemplateManagerImpl#listByQuery(com.greenline.push.dal.query.TemplateQuery)
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public TemplateQuery listByQuery(TemplateQuery query) {
		templateDao.listByQuery(query);
		List<TemplateDO> list =query.getDataList();
		for(TemplateDO template : list){
			CategoryDO object = categoryManager.getCategoryById(template.getCategoryId());
			template.setCategoryName(object == null?"":object.getCategoryName());
		}
		return  query;
	}

	/* (non-Javadoc)
	 * @see com.greenline.push.core.pushcenter.TemplateManagerImpl#delete(java.lang.Long)
	 */
	@Override
	public boolean delete(Long id) {
		return templateDao.deleteById(id) > 0;
	}

	/* (non-Javadoc)
	 * @see com.greenline.push.core.pushcenter.TemplateManagerImpl#listAll()
	 */
	@Override
	public List<TemplateDO> listAll() {
		return templateDao.listAll();
	}

    @Override
    public TemplateDO getByCode(String code) {
        return templateDao.getByCode(code);
    }
}
